import { INIT_LIST, DEL_ITEM, SERAC_LIST, ADD_ITEM , CHANGE_ITEM} from '../actions/list'

function list(state = [], { type, res, index, value, item, editItem }) {
    let NewState = JSON.parse(JSON.stringify(state))
    switch(type) {
        case INIT_LIST:
            NewState = res
            return NewState
        case DEL_ITEM:
            NewState.splice(index, 1)
            return NewState
        case SERAC_LIST:
            NewState = NewState.filter(item => item.name.includes(value))
            return NewState
        case ADD_ITEM:
            NewState.push(item)
            return NewState
        case CHANGE_ITEM:
            // NewState = NewState.map(item => {
            //     // 要替换的数据
            //     if (item.key === editItem.key) {
            //         return editItem
            //     } else {
            //         return item
            //     }
            // })
            NewState = NewState.map(item => item.key === editItem.key? editItem: item)
            return NewState
        default:
            return state
    }
}

export default list